﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Model;

namespace LaKuaShop_TestDemo
{
    /// <summary>
    /// Manager.xaml 的交互逻辑
    /// </summary>
    public partial class Manager : Window
    {
        public List<ProductInfo> pro_list = new List<ProductInfo>();
        public Manager()
        {
            InitializeComponent();
            InitList();
        }
        //显示列表数据
        private void InitList()
        {
            BLL.Product Pro = new BLL.Product();
            pro_list = Pro.ShowProductList();
            list.ItemsSource = pro_list;
        }

        //修改商品信息
        private void EditButton(object sender, RoutedEventArgs e)
        {
            var mySelectedElement = (ProductInfo)list.SelectedItem;
            string selectedID = mySelectedElement.Pid;
            ChangeProduct w1 = new ChangeProduct(false, selectedID);
            w1.ShowDialog();
            InitList();
        }
        //添加商品信息
        private void Add_button_Click(object sender, RoutedEventArgs e)
        {
            ChangeProduct w1 = new ChangeProduct(true);
            w1.ShowDialog();
            InitList();
        }

        //删除商品信息
        private void Delete_Button(object sender, RoutedEventArgs e)
        {
            var mySelectedElement = (ProductInfo)list.SelectedItem;
            string selectedID = mySelectedElement.Pid;
            if (MessageBox.Show("确认删除?", "确认提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                BLL.Product Pro = new BLL.Product();
                Pro.DeleteProduct(selectedID);
                Pro.DeleteProductImg(selectedID);
                InitList();
            }
        }

        private void Exit(object sender, RoutedEventArgs e)
        {
            Common.User = new UserInfo();
            Login win = new Login();
            var window = Window.GetWindow(this);
            window.Close();
            win.ShowDialog();
        }
        //选择数据处理方式
        private void Choose(object sender, RoutedEventArgs e)
        {
            string order = OrderChoose.Text;
            string tag = Tagchoose.Text;
            if(order == "" && tag == "")
            {
                return;
            }else if( order != "" && tag == "")
            {
                string method = (order == "价格升序") ? "desc" : "asc";
                BLL.Product Pro = new BLL.Product();
                pro_list = Pro.ShowProductList();
                pro_list = Pro.ProductOrder(method, pro_list);
                list.ItemsSource = pro_list;
            }
            else if(tag != "" && order == "")
            {
                BLL.Product Pro = new BLL.Product();
                pro_list = Pro.ShowProListByTag(tag);
                list.ItemsSource = pro_list;
                return;
            }
            else
            {
                string method = (order == "价格升序") ? "desc" : "asc";
                BLL.Product Pro = new BLL.Product();
                pro_list = Pro.ShowProListByTag(tag);
                pro_list = Pro.ProductOrder(method, pro_list);
                list.ItemsSource = pro_list;
                return;
            }
        }
    }
}
